<style>
body {
  background-image: url("../../public/loginImage.png");
  background-size: cover; /*设置封面*/
}

.layout-main {
  text-align: center;
  height: 1149px;
}

/*表单字体*/
.el-form-item__label {
  color: white;
  font-size: 16px;
}

/*分割线*/
.el-divider--horizontal {
  width: 76%;
  margin: 24px 600px;
}

.el-divider__text {
  font-size: 23px;
  font-family: 华文中宋;
  background-color: #ffd300;
}
</style>
<template>
  <div>
    <el-page-header style="background: rgba(255, 255, 255, 0.5); color: white;
    line-height: 60px; font-weight: bold" @back="goBack" content="返回登录页面">
    </el-page-header>
    <el-container>
      <el-main class="layout-main">
        <span style="color: white;font-size: 45px;font-family: 华文中宋;font-weight: bold">为了无法计算的价值!</span>
        <el-divider content-position="right" style="font-size: 20px">阿里云</el-divider>
        <el-card style="width: 560px;height: 600px;margin: 300px 1400px;
                background-color: rgba(255,255,255,0.3)">
          <!--label-width设置用户名这一列所占的宽度,如果不设置会显示在上面-->
          <h1 style="font-family: 华文中宋;font-weight: bold;font-size: 30px;color: white">用户注册</h1>
          <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px"
                   style="width: 400px;margin: 40px auto">
            <el-form-item label="昵称" prop="nickname">
              <el-input type="nickname" v-model="ruleForm.nickname" placeholder="请输入昵称"></el-input>
            </el-form-item>
            <el-form-item label="用户名" prop="username">
              <el-input type="username" v-model="ruleForm.username" placeholder="请输入用户名"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="password">
              <el-input type="password" v-model="ruleForm.password" placeholder="请输入密码"></el-input>
            </el-form-item>
            <el-form-item label="确认密码">
              <el-input type="password" v-model="passwordToo" @blur="f()" placeholder="请再次输入密码"></el-input>
            </el-form-item>
            <el-form-item label="电子邮箱" prop="email">
              <el-input type="email" v-model="ruleForm.email" placeholder="请输入电子邮箱"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button style="position: relative;right: 30px;margin-top: 20px"
                         type="primary" @click="submitForm('ruleForm')">注册
              </el-button>
              <el-button @click="resetForm('ruleForm')">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </el-main>
    </el-container>
  </div>
</template>
<script>
export default {
  data() {
    return {
      passwordToo: '',
      ruleForm: {// 初始化ruleForm对象
        nickname: '',
        username: '',
        password: '',
        email: '',
      },
      rules: { // 制定规则
        nickname: [
          {required: true, message: '请输入昵称', trigger: 'blur'},
        ],
        username: [
          {required: true, message: '请输入账号', trigger: 'blur'},
          {min: 4, max: 15, message: '长度在 4 到 15 个字符', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {min: 4, max: 15, message: '长度在 4 到 15 个字符', trigger: 'blur'}
        ],
        email: [
          {required: true, message: '请输入电子邮箱', trigger: 'blur'},
          {min: 8, max: 20, message: '长度在 8 到 20 个字符', trigger: 'blur'}
        ],
      }
    };
  },
  methods: {
    open1() {
      const h = this.$createElement;
      this.$notify({
        title: 'Welcome!',
        message: h('i', {style: 'color: green'}, 'Welcome to Wanpai Mall, please register first and start your new shopping trip!'),
        showClose: true
      });
    },
    // 处理注册事件
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = this.GLOBAL.productUrl + 'users/register';
          console.log('url=' + url);
          let formData = this.qs.stringify(this.ruleForm);
          this.axios.post(url, formData).then((response) => {
            let responseBody = response.data;
            if (responseBody.state == 20000) {
              this.$message({
                message: '注册成功!',
                type: 'success'
              });
              this.$router.push('/login');
            } else {
              this.$message.error(responseBody.message);
            }
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    f() {
      if (this.ruleForm.password !== this.passwordToo) {
        this.$message.error("两次输入的密码不一致，请重新输入！")
        this.passwordToo = '';
      }
    },
    goBack() {
      history.back();//返回上一页面
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.passwordToo = '';
    }
  },
  mounted() {
    this.open1();
  }
}
</script>